<?php 
namespace app\admin\controller;
use \think\Controller;
/**
* 团购有关设置
*/
class Groupbuy extends Base
{
	//团购卷展示列表
	public function groupbuy_list(){
		$groupbuy=model("TkGroupbuy");
		$dataobj=$groupbuy->select();
		foreach($dataobj as $v){
			$data[]=$v->toArray();
		}
		//当过期时间到后系统自动删除
		foreach ($data as $v) {
			$time=strtotime($v['die_time']);
			if(time()>$time){
				$groupbuy->destroy($v['id']);
				//echo $groupbuy->getLastSql();
			}
		}
		$dataobj=$groupbuy->select();
		$data=[];
		foreach($dataobj as $v){
			$data[]=$v->toArray();
		}
		$this->assign("data",$data);
		return $this->fetch();
	}

	//显示过期的团购卷
	public function del_groupbuy_list(){
		$groupbuy=model("TkDelGroupbuy");
		$dataobj=$groupbuy->select();
		foreach($dataobj as $v){
			$data[]=$v->toArray(); 
		}
		$this->assign("data",$data);
		return $this->fetch();
	}
	//删除过期团购卷
	public function del_groupbuy(){
		$id=input("param.id");
		$groupbuy=model("TkDelGroupbuy");
		$num=$groupbuy->destroy($id);
		if($num==1){
			$this->success("删除成功",'del_groupbuy_list','',1);
		}else{
			$this->error("请重新删除");
		}
	}
	//生成团购卷
	public function groupbuy_create(){
		//商户生成团购卷号 当卖家下订单的时候自动发放团购卷
		//团购卷号格式: xxxx xxxx xxxx
		//随机生成字符串
		$char='0123456789';
		$length=12;
		for ($i=0; $i <$length ; $i++) { 
			$str=$char[mt_rand(0,strlen($char)-1)];
			if($i%4==0){
				echo "&nbsp;";
			}
			echo $str;
			$this->assign("str",$str);
		}
		// exit;
 		return $this->fetch();
	}	
}
?>